import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/LoginPage.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

void main() {
  runApp(RegisterPage());
}
String username;
String password;

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPage createState() => new _RegisterPage();
}

class _RegisterPage extends State<RegisterPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomPadding:false,
        appBar: AppBar(
          title: Container(
            child: Text(
              '注 册',
              style: TextStyle(
                color: Colors.black,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          centerTitle:true,
          backgroundColor: Colors.white,
          titleSpacing: 20,
        ),
        body: new Container(
          padding: EdgeInsets.only(left: 0,top: 100,right: 0,bottom: 0),
          child: new ListView(
            children: [
              new Container(
                padding: const EdgeInsets.all(32.0),
                child: new Row(
                  children: [
                    new Expanded(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          new TextField(
                            decoration: InputDecoration(
                                contentPadding: EdgeInsets.all(10),
                                icon: Icon(Icons.account_circle_outlined),
                                hintText: '请输入用户名',
                                filled: true,
                                fillColor:Colors.transparent,
                                border: OutlineInputBorder(
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(15),
                                    ),
                                    borderSide: BorderSide(
                                      color: Colors.white,
                                    )
                                ),
                              isCollapsed: true,
                            ),
                            onChanged: (value){
                              username=value;
                            },
                          ),
                          SizedBox(height: 10),
                          new TextField(
                            decoration: InputDecoration(
                                contentPadding: EdgeInsets.all(10),
                                icon: Icon(Icons.lock_outline_rounded),
                                hintText: '请输入密码',
                                filled: true,
                                fillColor: Colors.transparent,
                                border: OutlineInputBorder(
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(15),
                                    ),
                                    borderSide: BorderSide(
                                      color: Colors.white,
                                    )
                                ),
                              isCollapsed: true,
                            ),
                            obscureText:true,
                            inputFormatters: [
                              LengthLimitingTextInputFormatter(6)
                            ],
                            onChanged: (value){
                              password=value;
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Jump()
            ],
          ),
          decoration: BoxDecoration(
              image: new DecorationImage(
                  fit: BoxFit.cover,
                  image: new AssetImage('images/paper6.jpg')
              )
          ),
        )
    );
  }
}
class Jump extends StatelessWidget{
  String mes;
  int code;
  Widget build(BuildContext context) {
    _postUsers() async{
      var url = "http://121.196.101.7:8080/treehole/user/register";
      var response = await http.post(url,body: {'username' : username,'password' : password});
      var result = json.decode(response.body);
      mes = result['message'];
      code = result['code'];
      if(code==0){
        Fluttertoast.showToast(
            msg: "注册成功",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 16.0
        );
        Navigator.pushReplacement(context, new MaterialPageRoute(builder: (context) => LoginPage()),);
      }
      else{
        Fluttertoast.showToast(
            msg: mes,
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.amber[50],
            textColor: Colors.black,
            fontSize: 16.0
        );
      }
    }
        return  Container(
            padding: const EdgeInsets.all(32.0),
            child: new Container(
              child: RaisedButton(
                child: new Text('注册',
                  style: TextStyle(
                    fontSize: 20,
                    color: Colors.black,
                    fontWeight: FontWeight.w300,
                  ),
                  textAlign: TextAlign.center,
                ),
                onPressed: _postUsers,
                color: Colors.green[100],
              ),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(25))
              ),
            )
        );
  }
}



